<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Muserecoupon extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    
    public function getEcouponUserDB($number,$offset)
    {
        $limit = $offset ? intval($offset) : '0';
        $sql  = '';
        $sql .= "SELECT ecp.id, ecp.code_coupon, ecp.coupon_id, ecp.user_id, ecp.created, ecp.active, cp.title, m.username, m.fullname
		FROM ecoupon_user AS ecp JOIN ecoupon AS cp ON ecp.coupon_id = cp.id JOIN member AS m ON ecp.user_id = m.id 
		WHERE cp.admin_id = ".$this->session->userdata('admin_id')." AND ecp.active > 0 AND user_id IS NOT NULL ";
       
        $key = $this->getParamString('keyword');
        if(!in_array($key, array(null, '', '0')))
        {
            $sql .= " AND (m.username like '%%".$key."%%' OR ";
            $sql .= " cp.title like like '%%".$key."%%') ";
        }
        $sql .= "ORDER BY ecp.id DESC";
        
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();
        $count = $this->db->query($sql.$xdata)->num_rows();
		//echo '<pre>';die(print_r($xs));
        return array('data' => $xs, 'count' => $count);   
    }
    
    
    public function getForm($coupon)
    {
        $data = $this->db->query('SELECT id,code_coupon,coupon_id,user_id,created,admin_id,active,time_use FROM ecoupon_user WHERE code_coupon = "'.$coupon.'" ' );
		$xs = $data->row_object();
		if( $data->num_rows() == 0 ){
			return array('status' => 202, 'data' => array(), 'content' => 'Coupon khong ton tai');
		}else{
			$coupon_id = $data->row()->coupon_id;
			$user_id = $data->row()->user_id;
				$xs->title = $this->db->query('SELECT title FROM ecoupon WHERE id = '.$coupon_id)->row()->title;
				$xs->sale_off = $this->db->query('SELECT sale_off FROM ecoupon WHERE id = '.$coupon_id)->row()->sale_off;
				$xs->condition = $this->db->query('SELECT `condition` FROM ecoupon WHERE id = '.$coupon_id)->row()->condition;
			if( !in_array($user_id,array('',null,0))){
				$xs->username = $this->db->query('SELECT username FROM member WHERE id = '.$user_id)->row()->username;
				$xs->fullname = $this->db->query('SELECT fullname FROM member WHERE id = '.$user_id)->row()->fullname;
				$xs->mobile = $this->db->query('SELECT mobile FROM member WHERE id = '.$user_id)->row()->mobile;
				$xs->user_active = $this->db->query('SELECT active FROM member WHERE id = '.$user_id)->row()->active;				
				return array('status' => 200, 'data' => $xs, 'content' => 'Chi tiet coupon');
			}else{
				return array('status' => 201, 'data' => $xs, 'content' => 'Coupon chua co nguoi nhan');
			}
			
		}
		//echo '<pre>';die(print_r($xs));
		return $xs;
    }
    
    public function selectMember($id)
    {
        $html = '';
        $sql = "SELECT id,username,fullname FROM member WHERE active = 1 ORDER BY id ASC ";
        $q = $this->db->query($sql);
        $data = $q->result_array();
        foreach($data AS $le=>$el)
        {
            if($id == $el['id'])
            {
                $selected = " selected = 'selected' ";
            }else{
                $selected = " ";
            }
            $html .= "<option value=".$el['id']." ".$selected.">".$el['username']." ---|--- ".$el['fullname']."</option>";
        }
        return $html;
    }
    
    public function addUserEcouponDB($id,$code_coupon,$active)
    {
        //$data['id_user']         = $this->getParamInt('user');
        //$data['id_coupon']       = $this->getParamInt('id');
        //$data['id_admin']        = $userID;
        //$data['created']         = date('Y-m-d H:i:s');
        $data = array( 
				'active' => $active,
				'admin_id' => $this->session->userdata('admin_id'),
				'time_use' => date('Y-m-d H:i:s')
			);
        $this->db->update('ecoupon_user', $data, "id = ".$id);
        return 200;
    }
    
    
    
}
